package com.zr.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.zr.dao.ZLCSuserdao;
import com.zr.model.Role;
import com.zr.model.User;
import com.zr.util.JDBCUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/*
 * zl
 * 得到所有的用户信息
 * */
public class ZLCSuserdaoimpl implements ZLCSuserdao{
	
	  
	  public List <User> alluser(int state,int rows){
		  
		  List <User> jsons = new ArrayList<>();
		  Connection con = JDBCUtil.getConnection();
		   
		try {
			
		     StringBuffer st = new StringBuffer();
		     st.append("select uid ,uname ,upsw, rname  ");
		     st.append("from e_user,e_role  ");
		     st.append("where e_user.rid=e_role.rid  ");
		     st.append("limit ?,?  ");
		     PreparedStatement pst = con.prepareStatement(st.toString());
		     pst.setInt(1, state);
		     pst.setInt(2, rows);
		     ResultSet rs = pst.executeQuery();
		     
		     while(rs.next())
		     {   
		    	 User j = new User();
		    	 j.setUid(rs.getInt("uid"));
		    	 j.setUname(rs.getString("uname"));
		    	 j.setUpsw(rs.getString("upsw"));
		    	 j.setRname(rs.getString("rname"));
		    	 jsons.add(j);
		     }
		    	 
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		  return jsons;
	  }
	  
	  public void  setuser(int uid ,int rid)
	  {
		   Connection con =JDBCUtil.getConnection();
		  
		try {
			 
		     StringBuffer st = new StringBuffer();
		     st.append("UPDATE   e_user set rid = ? where uid=?  ");
		     PreparedStatement pst = con.prepareStatement(st.toString());
		     pst.setInt(1, rid);
		     pst.setInt(2, uid);
		     pst.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  }
	  
	  public int  counts()
	  {
		   Connection con =JDBCUtil.getConnection();
		  int i=0;
		try {
			 
		     StringBuffer st = new StringBuffer();
		     st.append("select  count(*) as count from e_user   ");
		     PreparedStatement pst = con.prepareStatement(st.toString());
		     
		     ResultSet rs= pst.executeQuery();
		     while(rs.next())
		     {
		    	 i=rs.getInt("count");
		    	 
		     }
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return i;
	  }
}
